package com.spic.business.train.examDetail.controller;

import com.spic.business.train.examDetail.domain.TrainExamDetail;
import com.spic.business.train.examDetail.service.ITrainExamDetailService;
import com.spic.business.train.prepare.domain.TrainExamUserState;
import com.spic.common.core.domain.R;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.api.RemoteUserService;
import com.spic.system.api.domain.SysUser;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 考试详情记录Controller
 *
 * @author spic
 * @date 2022-07-13
 */
@RestController
@RequestMapping("/train/examDetail")
public class TrainExamDetailController extends BaseController
{
    @Autowired
    RemoteUserService userService;

    @Autowired
    private ITrainExamDetailService trainExamDetailService;

    /**
     * 查询考试详情记录列表
     */
    @PreAuthorize(hasPermi = "business/train:examrecord:list")
    @GetMapping("/list")
    public TableDataInfo list(TrainExamDetail trainExamDetail)
    {
        startPage();
        List<TrainExamDetail> list = trainExamDetailService.selectTrainExamDetailList(trainExamDetail);
        return getDataTable(list);
    }

    /**
     * 获取考试详情记录详细信息
     */
    @PreAuthorize(hasPermi = "business:examDetail:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(trainExamDetailService.selectTrainExamDetailById(id));
    }

    /**
     * 删除考试详情记录
     */
    @PreAuthorize(hasPermi = "business:examDetail:remove")
    @Log(title = "考试详情记录", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(trainExamDetailService.deleteTrainExamDetailByIds(ids));
    }



    /**
     * 查询考试详情记录列表
     */
    @GetMapping("/paperList")
    public TableDataInfo paperList(TrainExamDetail trainExamDetail)
    {
        startPage();
        List<TrainExamDetail> list = trainExamDetailService.selectPaperList(trainExamDetail);
        if(list !=null && list.size()>0){
            for(TrainExamDetail item: list){
                if(item.getDeptId() == null){
                    R<SysUser> userResp = userService.getUserById( Long.parseLong(item.getUserId()));
                    if (userResp != null && userResp.getCode() == HttpStatus.SC_OK) {
                        SysUser data = userResp.getData();
                        Long deptId = data == null ? null : data.getDeptId();
                        item.setDeptId(deptId);
                    }
                }
            }
        }
        return getDataTable(list);
    }



    /**
     * 删除考试详情记录
     */
    @PostMapping("/examNote")
    public AjaxResult examNote(@RequestBody HashMap<String,Object> map)
    {
        String[] split = map.get("ids").toString().split(",");
        String    note = map.get("note").toString();
        List<TrainExamUserState> list = new ArrayList<TrainExamUserState>();
        for (int i = 0; i < split.length; i++){
            TrainExamUserState item = new TrainExamUserState();
            item.setId( Long.parseLong(split[i]));
            item.setNote(note);
            list.add(item);
        }
        return toAjax(trainExamDetailService.examNote(list));
    }
}
